package com.cdtu.store.item.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdtu.store.common.entity.SkuEntity;
import com.cdtu.store.common.entity.SpuDetailEntity;
import com.cdtu.store.common.entity.SpuEntity;
import com.cdtu.store.common.utils.PageUtils;
import com.cdtu.store.common.utils.R;
import com.cdtu.store.item.service.GoodsService;
import com.cdtu.store.item.service.SkuService;
import com.cdtu.store.item.service.SpuDetailService;
import com.cdtu.store.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author dongyuhao
 */
@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuDetailService spuDetailService;
    @Autowired
    private SkuService skuService;
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils paeg = goodsService.list(params);
        return R.ok().put("data", paeg);
    }

    @PostMapping("/save")
    public R save(@RequestBody SpuEntity spu) {
        if (goodsService.saveGoods(spu)) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    @GetMapping("/spu/page")
    public Page<SpuEntity> querySpuByPage(@RequestParam Map<String, Object> params, @RequestParam(value = "saleable", defaultValue = "true") Integer saleable) {
        return this.spuService.querySpu(params, saleable);
    }

    /**
     * 根据spu商品id查询详情
     * @param id
     * @return
     */
    @GetMapping("/spu/detail/{id}")
    public SpuDetailEntity querySpuDetailById(@PathVariable("id") Long id){
        return this.spuDetailService.getOne(new QueryWrapper<SpuDetailEntity>().eq("spu_id",id));
    }

    /**
     * 根据spu的id查询sku
     * @param id
     * @return
     */
    @GetMapping("/sku/list")
    List<SkuEntity> querySkusBySpuId(@RequestParam("id") Long id){
        return this.skuService.list(new QueryWrapper<SkuEntity>().eq("spu_id",id));
    }

    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuEntity> querySpuById(@PathVariable("id") Long id){
        SpuEntity spu = this.goodsService.querySpuById(id);
        if(spu == null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(spu);
    }
    @GetMapping("/getGoods/{id}")
    public SpuEntity getGoods(@PathVariable("id")Long id){
        SpuEntity spu = this.goodsService.getGoods(id);
        return spu;
    }

    @PutMapping("/update")
    public R update(@RequestBody SpuEntity spu) {
        goodsService.UpdateGoods(spu);
        return R.ok();
    }

    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        this.goodsService.delete(ids);
        return R.ok();
    }

    @GetMapping("sku/{id}")
    public SkuEntity querySkuById(@PathVariable("id")Long id){
        return this.goodsService.querySkuById(id);
    }
}
